package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.AdminDao;
import cz.khfotbal.dao.ContactDao;
import cz.khfotbal.domain.Administrator;
import cz.khfotbal.domain.Contact;

@Repository
@Transactional
public class AdminDaoImpl implements AdminDao{

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;
	
	
	public List<Administrator> findAllAdmins() {
		session = sessionFactory.getCurrentSession();
		List<Administrator> list = session.createQuery("from Administrator").list();
		return list;
	}

	public void saveAdmin(Administrator admin) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(admin);
	}

	public void updateAdmin(Administrator admin) {
		session = sessionFactory.getCurrentSession();
		session.merge(admin);
	}

	public Administrator findAdminById(int id) {
		session = sessionFactory.getCurrentSession();
		Administrator admin = (Administrator) session.createQuery("from Administrator where id ="+id).uniqueResult();
		return admin;
	}

	public void deleteAdmin(Administrator admin) {
		session = sessionFactory.getCurrentSession();
		session.delete(admin);
	}

}
